import { defineStore } from 'pinia'

/**
 * 应用全局状态管理
 */
export const useAppStore = defineStore('app', {
  state: () => ({
    // 侧边栏状态
    sidebar: {
      opened: true,
      withoutAnimation: false
    },

    // 设备类型 - 固定为桌面端
    device: 'desktop',

    // 主题设置
    theme: {
      mode: 'light', // light, dark
      primaryColor: '#409EFF',
      layout: 'vertical' // vertical, horizontal
    },

    // 页面设置
    settings: {
      showBreadcrumb: true,
      showTabs: true,
      showFooter: true,
      fixedHeader: true,
      fixedSidebar: true
    },

    // 加载状态
    loading: false,

    // 系统信息
    systemInfo: {
      title: '校园运动会管理系统',
      version: '1.0.0',
      description: '基于Vue 3 + Element Plus的校园运动会管理系统'
    }
  }),

  getters: {
    // 获取侧边栏状态
    sidebarOpened: (state) => state.sidebar.opened,

    // 获取设备类型 - 只保留桌面端
    isDesktop: (state) => state.device === 'desktop',

    // 获取主题模式
    isDarkMode: (state) => state.theme.mode === 'dark',

    // 获取布局设置
    layoutSettings: (state) => state.settings,

    // 获取系统信息
    systemTitle: (state) => state.systemInfo.title,
    systemVersion: (state) => state.systemInfo.version
  },

  actions: {
    // 切换侧边栏
    toggleSidebar(withoutAnimation = false) {
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = withoutAnimation
    },

    // 关闭侧边栏
    closeSidebar(withoutAnimation = false) {
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },

    // 设置设备类型 - 固定为桌面端
    setDevice(device) {
      this.device = device || 'desktop'
    },

    // 切换主题模式
    toggleTheme() {
      this.theme.mode = this.theme.mode === 'light' ? 'dark' : 'light'
      this.applyTheme()
    },

    // 设置主题模式
    setTheme(mode) {
      this.theme.mode = mode
      this.applyTheme()
    },

    // 设置主色调
    setPrimaryColor(color) {
      this.theme.primaryColor = color
      this.applyTheme()
    },

    // 应用主题
    applyTheme() {
      const root = document.documentElement

      if (this.theme.mode === 'dark') {
        root.classList.add('dark')
      } else {
        root.classList.remove('dark')
      }

      // 设置主色调
      root.style.setProperty('--el-color-primary', this.theme.primaryColor)
    },

    // 更新设置
    updateSettings(newSettings) {
      this.settings = { ...this.settings, ...newSettings }
    },

    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },

    // 显示全局加载
    showLoading() {
      this.setLoading(true)
    },

    // 隐藏全局加载
    hideLoading() {
      this.setLoading(false)
    },

    // 设置系统信息
    setSystemInfo(info) {
      this.systemInfo = { ...this.systemInfo, ...info }
    },

    // 重置应用状态
    resetAppState() {
      this.sidebar = {
        opened: true,
        withoutAnimation: false
      }
      this.device = 'desktop' // 固定为桌面端
      this.theme = {
        mode: 'light',
        primaryColor: '#409EFF',
        layout: 'vertical'
      }
      this.settings = {
        showBreadcrumb: true,
        showTabs: true,
        showFooter: true,
        fixedHeader: true,
        fixedSidebar: true
      }
      this.loading = false
    }
  },

  // 持久化配置
  persist: {
    key: 'app-store',
    storage: localStorage,
    paths: ['sidebar', 'theme', 'settings', 'systemInfo']
  }
})